package app;
import info.clearthought.layout.TableLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.KeyStroke;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Dialog2 extends javax.swing.JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	
	JPopupMenu PopMenu=new JPopupMenu (); 
	
	   
	   
	   JMenuItem PopPegar=new JMenuItem ("Pegar");
	   
	   
	   public void setTipo(String str){
			this.setTexto("");
			
			if (str=="imagen"){
				tipo=1;
				centrado.setVisible(true);
				jLabel1.setText("URL de la imagen");
			}
			if (str=="link"){
				tipo=2;
				centrado.setVisible(false);
				jLabel1.setText("Texto del link");
			}
				
		}
	   
	   
	   
	
	
	
	private JTextField url;
	private JLabel urlLabel;
	private JTextField TextImg;

	private JLabel jLabel1;
	private JButton cancel;
	private JButton ok;
	public Main main;
	private int tipo;
	/*
	 * tipo=1 imagen
	 * tipo=2 link
	 */
	
	
	public Dialog2(JFrame frame) {
		super(frame);
		initGUI();
		addEscapeKey ();
		
	}
	
	
	
		
	public void setTexto(String str){
		
		TextImg.setText(str);
		
	}
	
	public void urlRequestFocus() {
		url.requestFocusInWindow();
	}
	
	private void addEscapeKey ()
	{
	// Handle escape key to close the dialog

	KeyStroke escape = KeyStroke.getKeyStroke (KeyEvent.VK_ESCAPE, 0, false);
	Action escapeAction = new AbstractAction ()
	{
	/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

	public void actionPerformed (ActionEvent e)
	{
		cancelActionPerformed(e);
	}
	};

	getRootPane ().getInputMap (JComponent.WHEN_IN_FOCUSED_WINDOW).put (escape, "ESCAPE");
	getRootPane ().getActionMap ().put ("ESCAPE", escapeAction);
	}
	
	
	
	public String getUrl(){
		return url.getText();
		
	}
	
	public void conectar(Main a1){
		main=a1;
		
	}
	private void pegarActionPerformed(ActionEvent evt) {
		
		if (PopMenu.getInvoker().getName()=="TextImg") 		
		TextImg.paste();
		else
			url.paste();
	}

	private void initGUI() {
		try {

			
	        PopMenu.add(PopPegar);
	        PopPegar.setIcon(new ImageIcon(getClass().getResource("paste.gif")));
	        PopPegar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					pegarActionPerformed(evt);
				}
			});
	        
	        
	        
	        TableLayout thisLayout = new TableLayout(new double[][] {{7.0, TableLayout.FILL, TableLayout.FILL, 7.0, 7.0}, {TableLayout.FILL, TableLayout.FILL, TableLayout.FILL, 7.0, TableLayout.FILL, 7.0}});
			thisLayout.setHGap(5);
			thisLayout.setVGap(5);
			getContentPane().setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(540, 185));
			getContentPane().setBackground(new java.awt.Color(255,255,255));
			this.setLocation(new java.awt.Point(100, 24));
			this.setTitle("Insertar imagen con link");
			this.setAlwaysOnTop(true);
			{
				TextImg = new JTextField();
				TextImg.setName("TextImg");
				TextImg.setComponentPopupMenu (PopMenu);
				getContentPane().add(TextImg, "2, 1");
				
			}
			{
				url = new JTextField();
				url.setName("url");
				getContentPane().add(url, "2, 2,");
				//url.setPreferredSize(new java.awt.Dimension(390, 123));
				url.setComponentPopupMenu (PopMenu);
				
				
			}
			
			{
				urlLabel = new JLabel();
				getContentPane().add(urlLabel, "1,2,c,f");
				urlLabel.setText("Link");
				urlLabel.setFont(new java.awt.Font("Arial",1,14));
			}
			{
				ok = new JButton();
				getContentPane().add(ok, "1,4,r,f");
				ok.setText("Aceptar");
				ok.setFont(new java.awt.Font("Ubuntu",0,12));
				ok.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						okActionPerformed(evt);
					}
				});
				getRootPane().setDefaultButton(ok);
				ok.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						okMouseClicked(evt);
					}
				});
			}
			{
				cancel = new JButton();
				getContentPane().add(cancel, "2,4,l,f");
				cancel.setText("Cancelar");
				cancel.setFont(new java.awt.Font("Ubuntu",0,12));
				cancel.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						cancelActionPerformed(evt);
					}
				});
			}
			
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1, "1,1,c,f");
				getContentPane().add(getCentrado(), "2,4,r,f");
				jLabel1.setText("URL de la imagen");
				jLabel1.setFont(new java.awt.Font("Arial",1,14));
			}
			
			this.setSize(540, 185);
			getPopup1();
			this.setIconImage(new ImageIcon(getClass().getClassLoader().getResource("app/postinga16x16.png")).getImage());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void setDefaultButton(){
		getRootPane().setDefaultButton(ok);
	}
	
	
	private void okMouseClicked(MouseEvent evt) {
		
		okAction();
	}
	
	private void okAction(){
		String link = null;
		link=url.getText();
		String img=TextImg.getText();
		link=link.toLowerCase();
		if( !link.equals(""))
		{
			if(tipo==1){ //tipo 1= imagenes
				
				main.generarImagenConLink(link,img,this.centrado.isSelected());
				
			}
			else //es un link
				
			{					
				if (!img.equals(""))	// existe texto del link
				{
					
									 main.generarLink(url.getText(),TextImg.getText()); 
				}
					else {			
				
				 main.generarLink(link,link);
					}
			}
		}
		
		
		
		url.setText("");
		TextImg.setText("");
		main.obtenerFoco();
		this.dispose();
		
	}
	
	
	
		
	private void okActionPerformed(ActionEvent evt) {
		okAction();
	}
	
	private void cancelActionPerformed(ActionEvent evt) {
		url.setText("");
		TextImg.setText("");
		main.obtenerFoco();
		this.dispose();
	}
	/********************************PopUp*******************************************/
	JPopupMenu popup1; 
	JMenuItem PopCortar1;
	JMenuItem PopPegar1;
	JMenuItem PopCopiar1;
	JMenuItem PopSelAll1;
	private String icon_copy="copy.gif";
	private JCheckBox centrado;
	private String icon_paste="paste.gif";
	private String icon_cut="cut.gif";
	private String icon_select_all="selectall.gif";
	
	
	private JMenuItem getPopCortar1() {
		if(PopCortar1 == null) {
			PopCortar1 = new JMenuItem("Cortar");
			PopCortar1.setIcon(new ImageIcon(getClass().getResource("cut.gif")));
			PopCortar1.setFont(new java.awt.Font("Ubuntu",0,12));
			PopCortar1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					PopCortar1ActionPerformed(evt);
				}
			});
		}
			return PopCortar1;
		}
	
	private JMenuItem getPopCopiar1() {
		if(PopCopiar1 == null) {
			PopCopiar1 = new JMenuItem("Copiar");
			PopCopiar1.setFont(new java.awt.Font("Ubuntu",0,12));
			PopCopiar1.setIcon(new ImageIcon(getClass().getResource("copy.gif")));
			PopCopiar1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					PopCopiar1ActionPerformed(evt);
				}
			});
		}
			return PopCopiar1;
		}
			
			
	private JMenuItem getPopPegar1() {
		if(PopPegar1 == null) {
			PopPegar1 = new JMenuItem("Pegar");
			PopPegar1.setIcon(new ImageIcon(getClass().getResource("paste.gif")));
			PopPegar1.setFont(new java.awt.Font("Ubuntu",0,12));
			PopPegar1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					PopPegar1ActionPerformed(evt);
				}
			});
		}
			return PopPegar1;
		}
	
	
	
	private JMenuItem getPopSelAll1() {
		if(PopSelAll1 == null) {
			PopSelAll1 = new JMenuItem("Seleccionar todo");
			PopSelAll1.setIcon(new ImageIcon(getClass().getResource(icon_select_all)));
			PopSelAll1.setFont(new java.awt.Font("Ubuntu",0,12));
			PopSelAll1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					PopSelAll1ActionPerformed(evt);
				}
			});	
		}
			return PopSelAll1;
		}
	
	private void PopPegar1ActionPerformed(ActionEvent evt) {
		if (popup1.getInvoker().getName()=="TextImg")
    	{	
			TextImg.paste();
			TextImg.requestFocus();
    		    	}
    	else
    	{
    		url.paste();
    		url.requestFocus();
    	}
    	
    }
    
    private void PopCopiar1ActionPerformed(ActionEvent evt) {
    	
    	if (popup1.getInvoker().getName()=="url")
    	{	
    		url.copy();
    		    	}
    	else
    	{ 
    		TextImg.copy();
    	}
    	
    }
    
    
    
    private void PopSelAll1ActionPerformed(ActionEvent evt) {
    
    	if (popup1.getInvoker().getName()=="url"){
	    	url.selectAll(); 
	    	url.requestFocus();		    	
}
else {

	TextImg.selectAll();
	TextImg.requestFocus();
}		    	
    }
    
	
    private void PopCortar1ActionPerformed(ActionEvent evt) {
    	if (popup1.getInvoker().getName()=="TextImg")
    	{
    		TextImg.cut();
    		TextImg.requestFocus();
    	}
    		else
    		{
    			url.cut();
    		url.requestFocus();
    		}
    	
	}
	
    private JPopupMenu getPopup1() {
		if(popup1 == null) {
			popup1 = new JPopupMenu();
			url.setComponentPopupMenu (popup1);
			TextImg.setComponentPopupMenu (popup1);
			popup1.add(getPopCortar1());
			popup1.add(getPopCopiar1());
			popup1.add(getPopPegar1());
			popup1.add(getPopSelAll1());
			
		}
		return popup1;
		}
    
    private JCheckBox getCentrado() {
    	if(centrado == null) {
    		centrado = new JCheckBox();
    		centrado.setText("Centrar");
    		centrado.setFont(new java.awt.Font("Arial",0,12));
    	}
    	return centrado;
    }

    /********************************PopUp*******************************************/
	
	
}
